<%--
  Created by IntelliJ IDEA.
  User: 70481
  Date: 2019/9/12
  Time: 21:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理员管理</title>
    <link href="${pageContext.request.contextPath}/layui/css/layui.css" rel="stylesheet" type="text/css">
</head>
<body>
<%--显示信息的table--%>
<table id="myTable" lay-filter="myTable"></table>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%--表单--%>

<form class="layui-form" action="" style="display: none;" id="myForm" lay-filter="myForm">
    <div class="layui-form-item">
        <input type="hidden" id="adminId" name="adminId" value="">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input type="text" name="adminName" lay-verify="title" lay-reqtext="账号不能为空？" autocomplete="off" placeholder="请输入账号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="adminPassword" lay-verify="required" lay-reqtext="密码不能为空？" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>



<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>


<script type="text/javascript">
    layui.use(["table","form","layer"],function () {
        var table= layui.table;
        var form = layui.form;
        var layer = layui.layer;


        // 表格的初始化
        table.render({
            elem:"#myTable",
            url:"${pageContext.request.contextPath}/admin/selectAdminPage",
            page:"true",
            toolbar:"#tool",
            cols:[[
                {checkbox:'checkbox'},
                {title:"编号",field:"adminId",width:65},
                {title:"管理员你账号",field:"adminName"},
                {title:"管理员密码",field:"adminPassword"},
                {title:"操作",toolbar: '#os'}
            ]]
        })

        // 监听头事件

        table.on("toolbar(myTable)",function (obj) {
            $("#adminId").val("")
            // alert(123)
            $("#myForm")[0].reset()
            layer.open({
                type:1,
                offset:"t",
                anim:1,
                area:"600px",
                content:$("#myForm")
            })
        })

        // 监听form提交
        form.on("submit(demo1)",function(data){
             console.log(data.field)
            if($("#adminId").val()===""){
                $.ajax({
                    url:"${pageContext.request.contextPath}/admin/insertAdmin",
                    data:data.field,
                    success:function (data) {
                        layer.msg(data.isInsert,{time:1250})
                        layer.closeAll("page"),
                        table.reload("myTable")
                    }
                })
            }else{
                $.ajax({
                    url:"${pageContext.request.contextPath}/admin/updateAdmin",
                    data:data.field,
                    success:function (data) {
                        layer.msg(data.isUpdate,{time:1250});
                        layer.closeAll("page");
                        table.reload("myTable");
                    },
                    error:function (data) {
                        layer.msg("没有权限",{time:1250})
                        layer.closeAll("page");
                    }
                })
            }

            return false;
        })


    })
// 修改
    function update(obj) {
        var form = layui.form;
        var layer = layui.layer;
        // alert(obj)
        $.ajax({
            url:"${pageContext.request.contextPath}/admin/selectById",
            data:{"adminId":obj},
            success:function (data) {
                console.log(data)
                form.val("myForm",{
                    adminId:data.adminId,
                    adminName:data.adminName,
                    adminPassword:data.adminPassword
                });
                layer.open({
                    type:1,
                    offset:"t",
                    anim:1,
                    area:"600px",
                    content:$("#myForm")
                })

            }
        })
    }

    function dele(obj) {
        var layer = layui.layer;
        var table = layui.table;

        layer.confirm("确定删除吗？",function (index) {
            $.ajax({
                url:"${pageContext.request.contextPath}/admin/deleteAdmin",
                data:{"adminId":obj},
                success:function(data){
                    layer.msg(data.isDelete,{time:1250})
                    table.reload("myTable")
                }
            })
        })
    }
</script>

<script type="text/html" id="os">
    <%--只有超级超级管理员才能看见的操作,其他的管理员只能进行添加--%>
    <shiro:hasRole name="supersuperadmin">
        <button type="button" class="layui-btn  layui-btn-sm layui-btn-radius layui-btn-normal" lay-event="update" id="update" onclick="update('{{d.adminId}}')">
            <i class="layui-icon layui-icon-util"></i>修改</button>
        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger layui-btn-radius" lay-event="delete"  onclick="dele('{{d.adminId}}')">
            <i class="layui-icon layui-icon-delete"></i>删除</button>
    </shiro:hasRole>

</script>
<script type="text/html" id="tool">
    <button type="button" class="layui-btn  layui-btn-sm layui-btn-radius layui-btn-normal" lay-event="add">
        <i class="layui-icon layui-icon-add-circle-fine"></i>上传</button>
</script>

</html>
